<template>
  <div class="app-container">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px" style="margin-left: 70px;overflow-y:auto;position: relative" >
<!--        <span style="font-size: 18px;">编辑安全隐患监督台账</span>-->
<!--        <el-divider></el-divider>-->
        <el-row>
          <el-col :span="8" class="elcol">
            <el-form-item label="检查日期:" prop="checkDate" >
              <el-date-picker clearable size="small" style="width: 200px"
                v-model="form.checkDate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="选择检查日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8" class="elcol">
            <el-form-item label="检查类型:" prop="checkType">{{typeFormat(form.checkType)}}
            </el-form-item>
          </el-col>
          <el-col :span="8" class="elcol">
            <el-form-item label="检查人：" prop="personInCharge">
              <el-input v-model="form.personInCharge"   placeholder="请输入检查人" maxlength="60"  show-word-limit style="width: 100%"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
<!--        日常检查-->
        <div style="overflow-y:auto;height:680px;">
          <div v-for="(item,index) in form.dailies" :key="item.id">
            <el-form-item v-model="item.checkContent"  style="margin-left: -70px;margin-bottom: 0px"><span style="font-size: 16px;font-weight: 700">{{index+1}}、{{item.checkContent}}</span></el-form-item>
            <el-row style="margin-left: 88px;">
              <el-col :span="8" class="elcol">
                <el-form-item  label="是否存在问题：" style="margin-bottom: 0px">
                  <el-radio-group  v-model="item.isProblems">
                    <el-radio
                      v-for="dict in dictList"
                      :key="dict.dictValue"
                      :label="dict.dictValue"
                    >{{dict.dictLabel}}</el-radio>
                  </el-radio-group>
                </el-form-item>
              </el-col>
              <el-col :span="16">
                <div v-show="item.isProblems == 1">
                  <el-row>
                    <el-col :span="8" STYLE="width: 35.3%" >
                      <el-form-item label="整改情况:"  style="margin-bottom: 0px">
                        <el-radio-group v-model="item.rectificationType">
                          <el-radio
                            v-for="dict in rectificationOptions"
                            :key="dict.dictValue"
                            :label="dict.dictValue"
                          >{{dict.dictLabel}}</el-radio>
                        </el-radio-group>
                      </el-form-item>
                    </el-col>
                    <el-col :span="8" class="elcol">
                      <el-form-item prop="termDate" v-show="item.rectificationType==1 && item.isProblems ==1" style="margin-bottom: 0px;">
                        <el-date-picker clearable size="small" style="width: 200px;margin-bottom: 0px"
                                        v-model="item.termDate"
                                        type="date"
                                        value-format="yyyy-MM-dd"
                                        placeholder="选择期限日期">
                        </el-date-picker>
                      </el-form-item>
                    </el-col>
                  </el-row>
                </div>
              </el-col>
            </el-row>
            <el-form-item label="描述:" style="margin-left: 24px;margin-bottom: 0px;"></el-form-item>
            <el-form-item style="margin-left: -20px;">
<!--              <el-input v-model="item.description" type="textarea" :rows="10" style="width: 65%;"></el-input>-->
              <textarea v-model="item.description" class="add_textarea" style="width: 65%;height: 230px"></textarea>
            </el-form-item>
            <el-form-item style="margin-left: -20px;">
              <el-upload
                :multiple="true"
                accept=".jpg, .png, .gif, .bmp, .jpeg"
                :headers="upload.headers"
                :action="upload.url"
                :file-list="item.imgList"
                list-type="picture-card"
                :limit="upload.limit"
                :before-upload="beforeImageUpload"
                :on-exceed="exceedFile"
                :on-success="(response, file, fileList)=>handlePictureSuccess2(response, file, fileList,item)"
                :on-preview="handlePictureCardPreview"
                :on-remove="(file, fileList)=>handleRemove2(file, fileList,item)">
                <i class="el-icon-plus"></i>
                <div slot="tip" class="el-upload__tip">只能上传jpg/png/bmp/jpeg文件，且不超过10M</div>
              </el-upload>
            </el-form-item>
          </div>
        </div>
      </el-form>
      <div slot="footer" style="float: right;margin-right: 20%" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    <el-dialog :visible.sync="dialogVisible">
      <img width="100%" :src="dialogImageUrl" alt="">
    </el-dialog>

  </div>
</template>

<script>
import { listInspection, getInspection, delInspection, addInspection, updateInspection, exportInspection,exportInspectionDetail,listInspectionConttent } from "@/api/ledger/inspection";
import { getToken } from "@/utils/auth";
export default {
  name: "Inspection",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      value: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,

      // 安全管理监督检查台账表格数据
      inspection: {},
      inspectionContentList: [],
      typeOptions:[],
      typeOptionList:[],
      rectificationOptions:[{"dictValue":"0","dictLabel":"现场整改"},{"dictValue":"1","dictLabel":"期限整改"}],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      is_show: false,
      termDate_show: false,
      rectification_show: false,
      openView: false,
      openRectification: false,
      station_btn: false,
      tableHeight: window.innerHeight - 275,
      //日期范围
      dateRange: [],
      srcList:[],
      checkSrcList:[],
      dictList:[],
      // 表单参数
      form: {
        id: undefined,
        stationId: undefined,
        checkDate: undefined,
        checkCompany: undefined,
        personInCharge: undefined,
        checkDeptWorker: undefined,
        existingProblems: undefined,
        rectificationOpinion: undefined,
        rectificationResults: undefined,
        reinspectionPerson: undefined,
        reinspectionPersonDate: undefined,
        delFlag: undefined,
        createTime: undefined,
        createId: undefined,
        updateTime: undefined,
        updateId: undefined,
        remark: undefined,
        checkType: undefined,
        checkObject: undefined,
        checkContent: undefined,
        isProblems: undefined,
        isRectification: undefined,
        dailies:[],
        problem: [],
        rectification: [],
        termDate: [],
        content: [],
        description: [],
        imgList2: [],
        imgList: [],
        imgs2:[],
        imgs:[],
        checkImgsList: [],
        checkImgs:[],
      },
      upload: {
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/common/upload",
        //  文件个数
        limit: 5
      },
      // 表单校验
      rules: {
        checkDate: [
          { required: true, message: "检查日期不能为空", trigger: "blur" }
        ],
      },
      id:undefined,
      dialogImageUrl: '',
      dialogVisible: false,};
  },
  watch: {
    $route: function(to,from){
      //do something
      this.getInspection();
    },
  },
  created() {
    this.reset();
    const id = this.$route.params && this.$route.params.id;
    this.id = id;
    this.getInspection();


    this.dictList = [{"dictValue":"1","dictLabel":"是"},{"dictValue":"0","dictLabel":"否"}]
  },
  methods: {
    getInspection(){
      getInspection(this.id).then(response =>{
        this.form = response.data;
        console.log(this.form.dailies)
        this.form.dailies.forEach(response=>{
          const imgs = [];
          response.imgList.map(function (item) {
            imgs.push(item.url)
          })
          response.imgs = imgs;
        })
      })
    },
    // 取消按钮
    cancel() {
      console.log("cancel");
      // this.$router.push({path: "/inspection/index", meta: { title: "aaa" }});
      this.$store.dispatch("tagsView/delView", this.$route);
      this.toLastView();
    },
    toLastView() {
      const latestView = this.$store.state.tagsView.visitedViews.slice(-1)[0];
      console.log(latestView.path);
      if (latestView) {
        this.$router.push(latestView);
      } else {
        // now the default is to redirect to the home page if there is no tags-view,
        // you can adjust it according to your needs.
        this.$router.push("/");
      }
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        stationId: undefined,
        checkDate: undefined,
        checkCompany: undefined,
        personInCharge: undefined,
        checkDeptWorker: undefined,
        existingProblems: undefined,
        rectificationOpinion: undefined,
        rectificationResults: undefined,
        reinspectionPerson: undefined,
        reinspectionPersonDate: undefined,
        delFlag: undefined,
        createTime: undefined,
        createId: undefined,
        updateTime: undefined,
        updateId: undefined,
        remark: undefined,
        checkType: undefined,
        checkObject: undefined,
        checkContent: undefined,
        isProblems: undefined,
        isRectification: undefined,
        imgList2: [],
        imgList: [],
        imgs2:[],
        imgs:[],
        checkImgsList: [],
        checkImgs:[],
      };
      this.resetForm("form");
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        // 日常检查
          if (valid) {
            if (this.form.id != undefined) {
              updateInspection(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("修改成功");
                  // this.$router.push({path: "/inspection/index", meta: { title: "aaa" }});
                  this.$store.dispatch("tagsView/delView", this.$route);
                  this.toLastView();
                } else {
                  this.msgError(response.msg);
                }
              });
            }
          }
      });
    },
    beforeImageUpload(file){
      const isLt2M = file.size / 1024 / 1024 < 10
      if (!isLt2M) {
        this.$message.error('上传图片大小不能超过 10MB!')
        return false
      }
    },
    exceedFile(files, fileList) {
      let msg = `最多只能上传${this.upload.limit}个图片`;
      this.$message.warning(msg)
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    //图片上传
    handleRemove2(file, fileList,item) {
      item.imgs.splice(item.imgs.indexOf(file.url),1);
    },
    handlePictureSuccess2(response, file, fileList,item) {
      if (item.imgs === undefined){
        item.imgs = [];
      }
      item.imgs.push(response.fileName);
    },
    handleChange2(value,item){
      if (value == '0'){
        item.termDate = '';
      }
    },
    handleChange3(value,item){
      if (value == '0'){
        item.termDate = '';
      }
    },
    // 检查类型
    typeFormat(val) {
      if (val === '0')
        return "日常检查"
      if (val === '1')
        return "专项检查"
    },
  }
};
</script>
<style>
  .el-upload--picture-card{
    width: 120px;
    height: 120px;
  }
  .el-upload-list--picture-card .el-upload-list__item {
    width: 120px;
    height: 120px;
  }
</style>
<style scoped>
  .img ul{
    padding: 0;
  }
  .img ul li{
    float:left;
    width:20%;
    list-style:none;
    width: 146px;
    margin-right: 2%;
  }
  .elcol{
    width: 24.333333%;
  }
  .add_textarea{
    display: block;
    resize: vertical;
    padding: 5px 15px;
    line-height: 1.5;
    box-sizing: border-box;
    width: 100%;
    font-size: inherit;
    color: #606266;
    background-color: #fff;
    background-image: none;
    border: 1px solid #DCDFE6;
    border-radius: 4px;
    outline-color:#b3e5fc;
    transition: border-color 0.2s cubic-bezier(0.645, 0.045, 0.355, 1);
  }
  @import '../../../assets/styles/ledger_style.scss';
</style>

